<template>
  <div class="main-audio-content">
    <div class="win-flex-row mb10">
      <el-row :gutter="12">
        <el-col :span="1.5">
          <el-button type="danger" size="small" :disabled="multiple" @click="handleDelete">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" size="small" @click="handleAdd">新增</el-button>
        </el-col>
      </el-row>
    </div>
    <el-col :span="24" :xs="24" style="height:calc(100vh - 178px)">
      <el-table @selection-change="handleSelectionChange" height="calc(100% - 64px)" :row-key="rowKey"
        :data="entityList" border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="name" label="名称" width="140">
        </el-table-column>
        <el-table-column prop="content" label="内容">
          <template slot-scope="scope">
            <div v-html="scope.row.content"></div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button class="delete-button" size="small" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="话术名称" prop="itemName">
              <el-input v-model="form.itemName" placeholder="请输入话术名称" /></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="话术内容" prop="scriptContent">
              <el-input v-model="form.scriptContent" type="textarea" :rows="8"
                placeholder="请输入话术内容"></el-input></el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="small">取 消</el-button>
        <el-button type="primary" size="small" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { saveEntity, getEntity, deleteEntity } from '@/api/script/item';
export default {
  name: 'MainAudio',
  components: {
  },
  props: {
    fileList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      entityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      // 表单参数
      form: {},
      // 非多个禁用
      multiple: true,
      // 表单校验
      rules: {
        itemName: [{ required: true, message: '话术名称不能为空', trigger: 'blur' }],
        scriptContent: [{ required: true, message: '话术内容不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {
  },
  computed: {
  },
  watch: {
    fileList: {
      handler(val) {
        this.entityList = val;
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    rowKey(row) {
      return row.id;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemName: '',
        scriptContent: '',
        scriptCategoryId: this.scriptCategoryId,
        scriptType: 'mainAudio'
      };
    },
    /** 提交按钮 */
    submitForm: _.debounce(function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$modal.loading('正在加载');
          saveEntity(this.form).then(res => {
            this.$modal.msgSuccess('操作成功');
            this.$modal.closeLoading();
            this.open = false;
            this.$emit('getList');

          });
        }
      });
    }, 80),
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getEntity(row.id).then(res => {
        this.form = res.result;
        this.open = true;
        this.title = '修改话术';
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加话术';
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return deleteEntity(ids);
      }).then(() => {
        this.$emit('getList');
        this.$modal.msgSuccess('删除成功');
      });
    },
  }
};
</script>

<style lang="scss" scoped></style>
